package com.ruoyi.web.controller.test2;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SharedFile;
import com.ruoyi.system.service.ISharedFileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 共享文件Controller
 * 
 * @author 王帝
 * @date 2022-08-02
 */
@RestController
@RequestMapping("/system/file")
public class SharedFileController extends BaseController
{
    @Autowired
    private ISharedFileService sharedFileService;

    /**
     * 导入数据管理列表
     */
    @Log(title = "数据管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SharedFile> util = new ExcelUtil<SharedFile>(SharedFile.class);
        List<SharedFile> dataManagementList = util.importExcel(file.getInputStream());
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = getUsername();
        String message = sharedFileService.importData(dataManagementList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<SharedFile> util = new ExcelUtil<SharedFile>(SharedFile.class);
        return util.importTemplateExcel("案卷数据");
    }



    /**
     * 查询共享文件列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(SharedFile sharedFile)
    {
        startPage();
        List<SharedFile> list = sharedFileService.selectSharedFileList(sharedFile);
        return getDataTable(list);
    }

    /**
     * 导出共享文件列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "共享文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SharedFile sharedFile)
    {
        List<SharedFile> list = sharedFileService.selectSharedFileList(sharedFile);
        ExcelUtil<SharedFile> util = new ExcelUtil<SharedFile>(SharedFile.class);
        util.exportExcel(response, list, "共享文件数据");
    }

    /**
     * 获取共享文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{shareId}")
    public AjaxResult getInfo(@PathVariable("shareId") Integer shareId)
    {
        return AjaxResult.success(sharedFileService.selectSharedFileByShareId(shareId));
    }

    /**
     * 新增共享文件
     */
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "共享文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SharedFile sharedFile)
    {
        sharedFile.setUserId(getUserId());
        sharedFile.setDeptId(getDeptId());
        System.out.println("sharedFile = " + sharedFile);
        return toAjax(sharedFileService.insertSharedFile(sharedFile));
    }

    /**
     * 修改共享文件
     */
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "共享文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SharedFile sharedFile)
    {
        return toAjax(sharedFileService.updateSharedFile(sharedFile));
    }

    /**
     * 删除共享文件
     */
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "共享文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shareIds}")
    public AjaxResult remove(@PathVariable Integer[] shareIds)
    {
        return toAjax(sharedFileService.deleteSharedFileByShareIds(shareIds));
    }
}
